package com.sudo.boot.admin.listener;

import com.sudo.boot.common.util.task.BaseTaskRegistry;
import com.sudo.boot.dingtalk.util.DingTalkUtil;
import com.sudo.boot.grpc.annotation.TaskComponent;
import jakarta.annotation.Resource;
import org.springframework.aop.support.AopUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 启动后加载指定bean
 */
@Component
public class BootApplicationListener implements ApplicationListener<ContextRefreshedEvent> {

    @Resource
    private DingTalkUtil dingTalkUtil;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        // 根容器为Spring容器
        ApplicationContext applicationContext = event.getApplicationContext();
        if (applicationContext.getParent() == null) {
            Map<String, Object> taskComponentMap = applicationContext.getBeansWithAnnotation(TaskComponent.class);

            taskComponentMap.forEach((k,v) -> {
                Class<?> targetClass = AopUtils.getTargetClass(v);
                BaseTaskRegistry.setModuleClassMap(k, targetClass);
            });
        }
        dingTalkUtil.send("----- Fxw Boot启动成功 -----");
    }
}
